package nowcoder;

/**
 * @author ：徐梦轲
 * @date ：2022/5/23 0023 9:44
 */
class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}

public class Partition {
    public ListNode partition(ListNode pHead, int x) {
        ListNode node1 = new ListNode(0);
        ListNode node2 = new ListNode(0);
        ListNode cur1 = node1;
        ListNode cur2 = node2;
        while (pHead != null) {
            if (pHead.val < x) {
                cur1.next = pHead;
                cur1 = pHead;
                pHead = pHead.next;
            } else {
                cur2.next = pHead;
                cur2 = pHead;
                pHead = pHead.next;
            }
        }

        cur1.next = node2.next;
        node2.next = null;
        cur2.next = null;
        return node1.next;
    }
}
